package thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class demo4 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        myCallable myCallable = new myCallable(); // 1. 这个类的实力,无法直接给到Thread
        FutureTask<Integer> integerFutureTask = new FutureTask<>(myCallable); // 2. 中间转化
        Thread thread = new Thread(integerFutureTask);
        thread.start();
        for (int i = 0; i < 2000; i++) {
            System.out.println("主线程");
        }

        System.out.println(integerFutureTask.get());
    }
}

//1. 编写一个类实现Callable接口
class myCallable implements Callable<Integer>{
    @Override
    public Integer call() throws Exception {//2. 重写call方法
//        3. 将线程任务代码写在call方法中
        int count = 0;
        for (int i = 1; i <= 100; i++) {
            count+=i;
            System.out.println(count+"线程任务");
        }
        return count;
    }
}